package com.sgb.knowledgeshare.view.fragment;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareMinfansListBinding;
import com.sgb.knowledgeshare.entity.FollowListEntity;
import com.sgb.knowledgeshare.view.activity.ShareMinFansActivity;
import com.sgb.knowledgeshare.view.activity.ShareUserActivity;
import com.sgb.knowledgeshare.view.adapter.ShareMinFansLisAdapter;
import com.sgb.knowledgeshare.viewmodel.ShareMinFansListModel;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;
import java.util.List;

/**
 * Created by fq on 2020/9/11 0011
 * Description：分享-我-粉丝和关注列表展示
 */
public class ShareMinFansListFragment extends AppLazyFragment {
    ShareMinFansListModel viewModel;
    private static String MINFANSLISTTYPE = "minfanslisttype";
    private FragmentShareMinfansListBinding binding;
    private String userid;
    private int MinFansType;
    private LRecyclerViewAdapter mLRecyclerViewAdapter;
    private ShareMinFansLisAdapter shareminfanslisadapter;
    private int page;
    private int pageSize;
    private boolean isFollowLoadMore;
    private boolean isFensLoadMore;
    private List<FollowListEntity.RecordsBean> fens;
    private List<FollowListEntity.RecordsBean> follow;

    public static ShareMinFansListFragment newInstance(int type) {
        ShareMinFansListFragment fragment = new ShareMinFansListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(MINFANSLISTTYPE, type);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_minfans_list, container, false);
        viewModel = new ViewModelProvider(this).get(ShareMinFansListModel.class);
        viewModel.setActivity(ShareMinFansListFragment.this);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        if (getArguments() != null) {
            MinFansType = getArguments().getInt(MINFANSLISTTYPE);
        }
        shareminfanslisadapter = new ShareMinFansLisAdapter(getActivity(), null,MinFansType);
        mLRecyclerViewAdapter = new LRecyclerViewAdapter(shareminfanslisadapter);
        binding.xrv.setAdapter(mLRecyclerViewAdapter);
        DividerDecoration divider = new DividerDecoration.Builder(getContext())
                .setHeight(R.dimen.dp3)
                .setColorResource(R.color.white)
                .build();
        binding.xrv.addItemDecoration(divider);
        binding.xrv.setLayoutManager(new LinearLayoutManager(getContext()));
        mLRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (MinFansType == 0) {//关注
                    //点击用户跳转到当前用户主页,加载用户主页信息
                    ShareUserActivity.start(getContext(), follow.get(position).getUserNo());
                } else {//粉丝
                    //点击用户跳转到当前用户主页,加载用户主页信息
                    ShareUserActivity.start(getContext(), fens.get(position).getUserNo());
                }

            }
        });
        binding.xrv.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                shareminfanslisadapter.getDatas().clear();
                initdata();
                mLRecyclerViewAdapter.notifyDataSetChanged();
            }
        });
        binding.xrv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (MinFansType == 0) {//关注
                    isFollowLoadMore = true;
                    page++;
                    viewModel.getFollowList(page, userid);
                } else {//粉丝
                    isFensLoadMore = true;
                    pageSize++;
                    viewModel.getFensList(pageSize, userid);
                }
            }
        });
        binding.xrv.refresh();
        shareminfanslisadapter.setFensData(new ShareMinFansLisAdapter.FensData() {
            @Override
            public void fens(String UserNo) {
                if (!SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "").equals(UserNo)) {
                    viewModel.getFens(UserNo);
                } else {
                    MToast.showToast(getContext(), "自己不能关注自己");
                }
            }
        });
    }

    private void initdata() {
        if (MinFansType == 0) {//关注
            isFollowLoadMore = false;
            page = 1;
            viewModel.getFollowList(page, userid);
        } else {//粉丝
            isFensLoadMore = false;
            pageSize = 1;
            viewModel.getFensList(pageSize, userid);
        }
    }

    @Override
    protected void onLazyLoad() {
        if (getArguments() != null) {
            MinFansType = getArguments().getInt(MINFANSLISTTYPE);
        }
        userid = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        initdata();
    }

    @Override
    public void onVisible() {
        if (getArguments() != null) {
            MinFansType = getArguments().getInt(MINFANSLISTTYPE);
        }
        userid = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, "");
        initdata();
    }

    //    关注的返回数据
    public void setFollowList(FollowListEntity data) {
        if (null == data) {
            return;
        }
        this.follow = data.getRecords();
        binding.xrv.refreshComplete(page);
        binding.xrv.setPullRefreshEnabled(true);
        if (isFollowLoadMore) {
            if (data.getRecords().size() > 0) {
                shareminfanslisadapter.addDatas(data.getRecords());
            } else {
                MToast.showToast(getContext(), "没有视频啦");
            }
        } else {
            if (data.getRecords().size() > 0) {
                binding.xrv.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                shareminfanslisadapter.setDatas(data.getRecords());
            } else {
                binding.xrv.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                shareminfanslisadapter.setDatas(null);
            }
        }
    }

    //粉丝的返回数据
    public void setFensList(FollowListEntity data) {
        if (null == data) {
            return;
        }
        this.fens = data.getRecords();
        binding.xrv.refreshComplete(pageSize);
        binding.xrv.setPullRefreshEnabled(true);
        if (isFensLoadMore) {
            if (data.getRecords().size() > 0) {
                shareminfanslisadapter.addDatas(data.getRecords());
            } else {
                MToast.showToast(getContext(), "没有视频啦");
            }
        } else {
            if (data.getRecords().size() > 0) {
                binding.xrv.setVisibility(View.VISIBLE);
                binding.empty.setVisibility(View.GONE);
                shareminfanslisadapter.setDatas(data.getRecords());
            } else {
                binding.xrv.setVisibility(View.GONE);
                binding.empty.setVisibility(View.VISIBLE);
                shareminfanslisadapter.setDatas(null);
            }
        }
    }

    //    关注和取消关注后的数据刷新
    public void setFens() {
        if(getActivity() != null){
            ((ShareMinFansActivity) getActivity()).updateFragmentViews();
        }
    }
}
